<?php
/**
 * 品牌管理
*
* @author karl <karl.tang@aiyuangong.com>
* @link http://www.aiyuangong.com
*/
namespace app\admin\controller\goods;

use app\admin\Controller;
use think\Db;
use think\Validate;

class GoodsBrand extends Controller
{
    use \app\admin\traits\controller\Controller;

    protected static $blacklist = [
        'deleteforever',
        'clear'
    ];
    
    protected function beforeIndex()
    {
        // 状态列表
        $stateList = [
            0 => '申请中',
            1 => '通过',
        ];
        $this->view->assign('state_list', $stateList);
    }
    
    public function index(){
        // 状态列表
        $stateList = [
            0 => '申请中',
            1 => '通过',
        ];
        $this->view->assign('state_list', $stateList);
        $name = $this->request->param('name/s','');
        $class_name = $this->request->param('class_name/s','');
        $where = [];
        if ($this::$isdelete !== false) {
            $where['isdelete'] = $this::$isdelete;
        }
        if($name){
            $where['name'] = ['like','%'.$name.'%'];
        }
        if($class_name){
            $where['class_name'] = ['like','%'.$class_name.'%'];
        }
        $list = $this->datalist(model('goods_brand'), $where);
        return $this->view->fetch();
    }

    /**
     * 品牌审核
     */
    public function auditOK()
    {
        return $this->updateField("apply", 1, "审核成功", '', '', 'goods_brand');
    }
    
    /**
     * 品牌保存
     */
    public function save()
    {
        $id = $this->request->param('id/d',0);
        $rule = [
            
            "name|品牌名称"      => "require|max:100|unique:goods_brand,name,".$id.',id',
            "initial|品牌首字母"      => "require|max:1",
            "pic|图片字段"      => "max:100",
            "sort|排序"      => "integer|between:0,255",
        ];
        $data['name'] = $this->request->param('name/s','');
        $data['initial'] = $this->request->param('initial/s','');
        $data['show_type'] = $this->request->param('show_type/d',0);
        $data['pic'] = $this->request->param('pic/s','');
        $data['recommend'] = $this->request->param('recommend/d',0);
        $data['sort'] = $this->request->param('sort/d',0);
        $validate = new Validate();
        if(!$validate->check($data, $rule)){
            return ajax_return_adv_error($validate->getError());
        }
        $src_img = '';
        if($data['pic']){
            $src_img = Db::name('goods_brand')->where(['id'=>$id])->field('pic')->find();
            $src_img = $src_img[0];
        }
        if($id){
            //编辑
            $result = Db::name('goods_brand')->where(['id'=>$id])->update($data);
        }else{
            //新增
            $result = Db::name('goods_brand')->insert($data);
        }
        if($result !== false){
            if(!empty($src_img)){
                @unlink(ROOT_PATH.$src_img);
            }
            return ajax_return_adv('保存成功','');
        }else{
            return ajax_return_adv_error('保存失败');
        }
    }
}